﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Lists;
using Sim;

namespace Sim
{
    public partial class Main : Form, IMain
    {
        public event DelegateLogin EventLogin = null;
        public event DelegateOpenSignUpWindow EventOpenSignUpWindow = null;
        public event DelegateCreateNewTournament EventNewTournament = null;
        public event DelegateRefreshTournamentList EventRefreshTournamentList = null;
        public event DelegateRefreshUserList EventRefreshUserList = null;
        public event DelegateApplyForTournament EventApplyForTournament = null;
        public event DelegateAcceptUserForTournament EventAcceptUserForTournament = null;
        public event DelegateDeclineUserForTournament EventDeclineUserForTournament = null;
        public event DelegateRemoveTournament EventRemoveTournament = null;
        public event DelegateStartTournament EventStartTournament = null;
        public event DelegateAddJudgeToTournament EventAddJudgeToTournament = null;
        public event DelegateSubmitDive EventSubmitDive = null;
        public event DelegateSubmitJudgeScore EventSubmitJudgeScore = null;
        private int indexUserList, indexTournamentList, indexApplyList;

        public Main()
        {
            InitializeComponent();
            tabMain.TabPages.Remove(tabPageAdmin);
            tabMain.TabPages.Remove(tabPageJudge);
            tabMain.TabPages.Remove(tabPageDiver);
            this.timerRefresh.Start();
            this.timerConnected.Start();
        }

        private void RefreshAdminTournamentListview()
        {
            listViewTournaments.Items.Clear();
            for (int i = 0; i < Program.ti.Count; i++)
            {
                ListViewItem lvi = new ListViewItem(Program.ti[i].isStarted.ToString());
                lvi.SubItems.Add(Program.ti[i].title);
                lvi.SubItems.Add(Program.ti[i].date);
                lvi.SubItems.Add(Program.ti[i].sex);
                lvi.SubItems.Add(Program.ti[i].jumpHeight.ToString());
                lvi.SubItems.Add(Program.ti[i].totalJudges.ToString());
                lvi.SubItems.Add(Program.ti[i].totalDivers.ToString());
                listViewTournaments.Items.Add(lvi);
            }
            try
            {
                listViewTournaments.Items[indexTournamentList].Selected = true;
            }
            catch (Exception)
            {
            }
        }

        private void RefreshJudgeContestInformation()
        {
            for (int i = 0; i < Program.ti.Count; i++)
            {
                if (Program.ti[i].isStarted == true && Program.cu.inTournament == Program.ti[i].title)
                {

                    labelJudgeContestNameInfo.Text = Program.ti[i].title;
                    labelJudgeDateInfo.Text = Program.ti[i].date;
                    labelJudgeHeightInfo.Text = Program.ti[i].jumpHeight.ToString();
                    labelJudgeGenderInfo.Text = Program.ti[i].sex;
                    labelJudgeTotalDiversInfo.Text = Program.ti[i].totalDivers.ToString();
                    listViewJudgeDiverList.Items.Clear();

                    for (int j = 0; j < Program.ti[i].diverList.Count; j++)
                    {

                        ListViewItem lvi = new ListViewItem(Program.ti[i].diverList[j].firstName + " " + Program.ti[i].diverList[j].lastName);
                        lvi.SubItems.Add(Program.ti[i].diverList[j].dives[Convert.ToInt32(Program.cu.round)].diveCode);
                        for (int k = 0; k < DDList.ddList.Count; k++)
                        {
                            if (DDList.ddList[k].diveCode == Program.ti[i].diverList[j].dives[Convert.ToInt32(Program.cu.round)].diveCode)
                            {
                                lvi.SubItems.Add(DDList.ddList[k].diveDescription);
                                break;
                            }
                        }
                        lvi.SubItems.Add(Program.ti[i].diverList[j].username);
                        listViewJudgeDiverList.Items.Add(lvi);
                    }
                    labelJudgeRounds.Text = "Round " + (Convert.ToInt32(Program.cu.round) + 1).ToString();
                }

                if (Program.ti[i].isStarted == false && Program.ti[i].isFinished == true && labelJudgeContestNameInfo.Text == "-")
                {
                    labelJudgeRounds.Text = "";
                }
                else if (Program.ti[i].isStarted == false && Program.ti[i].isFinished == true)
                    labelJudgeRounds.Text = "Finished!";
            }
        }


        private void RefreshJudgeScores()
        {
            for (int i = 0; i < Program.ti.Count; i++)
            {
                if (Program.ti[i].title == Program.cu.inTournament)
                {
                    for (int j = 0; j < Program.ti[i].judgeList.Count; j++)
                    {
                        Control judgeName = groupBoxJudgeScores.Controls[j];
                        Control judgeScore = groupBoxJudgeScores.Controls[j + 7];

                        if (j == 7)
                            break;
                        else
                        {
                            judgeName.Visible = true;
                            judgeScore.Visible = true;
                            judgeName.Text = Program.ti[i].judgeList[j].username;
                            for (int k = 0; k < Program.ti[i].diverList.Count; k++)
                            {
                                if (listViewJudgeDiverList.SelectedIndices.Count > 0)
                                {
                                    if (Program.ti[i].diverList[k].username ==
                                        listViewJudgeDiverList.SelectedItems[0].SubItems[3].Text)
                                    {
                                        judgeScore.Text = "-";
                                        for (int l = 0;
                                            l <
                                            Program.ti[i].diverList[k].dives[Convert.ToInt32(Program.cu.round)]
                                                .diveScore
                                                .Count;
                                            l++)
                                        {
                                            if (Program.ti[i].judgeList[j].username ==
                                                Program.ti[i].diverList[k].dives[Convert.ToInt32(Program.cu.round)]
                                                    .diveScore[l].usernameOfJudge)
                                            {
                                                judgeScore.Text = "X";
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private void RefreshAdminApplicationListview()
        {
            listViewApply.Items.Clear();
            for (int i = 0; i < Program.tr.Count; i++)
            {
                ListViewItem lvi = new ListViewItem(Program.tr[i].username);
                lvi.SubItems.Add(Program.tr[i].tournamentTitle);
                listViewApply.Items.Add(lvi);
            }
            try
            {
                listViewApply.Items[indexApplyList].Selected = true;
            }
            catch (Exception)
            {

            }
        }

        private void RefreshDiverTournamentListview()
        {
            listViewAvailableTournaments.Items.Clear();
            for (int i = 0; i < Program.ti.Count; i++)
            {
                if (Program.cu.gender == Program.ti[i].sex && Program.ti[i].title != Program.cu.inTournament && !Program.ti[i].isFinished && !Program.ti[i].isStarted)
                {
                    ListViewItem lvi = new ListViewItem(Program.ti[i].title);
                    lvi.SubItems.Add(Program.ti[i].date);
                    lvi.SubItems.Add(Program.ti[i].jumpHeight.ToString());
                    lvi.SubItems.Add((Convert.ToInt32(Program.ti[i].totalDivers) - Convert.ToInt32(Program.ti[i].diverList.Count)).ToString());
                    listViewAvailableTournaments.Items.Add(lvi);
                }
            }
        }

        private void RefreshDiverCurrentTournamentListview()
        {
            listViewDiverContestInfo.Items.Clear();
            for (int i = 0; i < Program.ti.Count; i++)
            {
                if (Program.cu.inTournament == Program.ti[i].title)
                {
                    labelDiverContestNameInfo.Text = Program.ti[i].title;
                    labelDiverDateInfo.Text = Program.ti[i].date;
                    labelDiverHeightInfo.Text = Program.ti[i].jumpHeight.ToString();
                    labelDiverGenderInfo.Text = Program.ti[i].sex;
                    labelDiverTotalDiversInfo.Text = Program.ti[i].totalDivers.ToString();

                    Program.ti[i].diverList.Sort((x, y) => y.totalScore.CompareTo(x.totalScore));

                    for (int j = 0; j < Program.ti[i].diverList.Count; j++)
                    {

                        ListViewItem lvi = new ListViewItem((j + 1).ToString());
                        lvi.SubItems.Add(Program.ti[i].diverList[j].firstName + " " + Program.ti[i].diverList[j].lastName);
                        lvi.SubItems.Add(Program.ti[i].diverList[j].nation);
                        lvi.SubItems.Add(Program.ti[i].diverList[j].totalScore.ToString());
                        listViewDiverContestInfo.Items.Add(lvi);
                    }
                    break;
                }
                else
                {
                    listViewDiverContestInfo.Items.Clear();
                    labelDiverContestNameInfo.Text = "-";
                    labelDiverDateInfo.Text = "-";
                    labelDiverHeightInfo.Text = "-";
                    labelDiverGenderInfo.Text = "-";
                    labelDiverTotalDiversInfo.Text = "-";
                }

            }
        }

        private void RefreshDiverScores()
        {

            for (int i = 0; i < Program.ti.Count; i++)
            {
                if (Program.ti[i].title == Program.cu.inTournament)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        Control diveCode = groupBoxDiverDives.Controls[j];
                        Control diveScore = groupBoxDiverDives.Controls[j + 6];

                        if (j > 5)
                            return;
                        else
                        {
                            for (int k = 0; k < Program.ti[i].diverList.Count; k++)
                            {
                                if (Program.cu.username == Program.ti[i].diverList[k].username)
                                {
                                    diveCode.Visible = true;
                                    diveCode.Text = Program.ti[i].diverList[k].dives[j].diveCode;
                                    diveScore.Visible = true;
                                    if (Program.ti[i].diverList[k].dives[j].calculatedScore != -1)
                                        diveScore.Text = Program.ti[i].diverList[k].dives[j].calculatedScore.ToString();
                                    break;
                                }
                            } 
                        }
                    }
                }
            }
        }

        private void RefreshAdminUserListview()
        {

            if (checkBoxAdminShowDivers.Checked == true && checkBoxAdminShowJudges.Checked == true)
            {
                listViewUsers.Items.Clear();
                for (int i = 0; i < Program.ui.Count; i++)
                {
                    ListViewItem lvi = new ListViewItem(Program.ui[i].id);
                    lvi.SubItems.Add(Program.ui[i].username);
                    lvi.SubItems.Add(Program.ui[i].firstname);
                    lvi.SubItems.Add(Program.ui[i].lastname);
                    lvi.SubItems.Add(Program.ui[i].age);
                    lvi.SubItems.Add(Program.ui[i].gender);
                    lvi.SubItems.Add(Program.ui[i].nation);
                    lvi.SubItems.Add(Program.ui[i].level);
                    listViewUsers.Items.Add(lvi);
                }
            }
            else if (checkBoxAdminShowJudges.Checked == true)
            {
                listViewUsers.Items.Clear();
                for (int i = 0; i < Program.ui.Count; i++)
                {
                    if (Program.ui[i].level == "1")
                    {
                        ListViewItem lvi = new ListViewItem(Program.ui[i].id);
                        lvi.SubItems.Add(Program.ui[i].username);
                        lvi.SubItems.Add(Program.ui[i].firstname);
                        lvi.SubItems.Add(Program.ui[i].lastname);
                        lvi.SubItems.Add(Program.ui[i].age);
                        lvi.SubItems.Add(Program.ui[i].gender);
                        lvi.SubItems.Add(Program.ui[i].nation);
                        lvi.SubItems.Add(Program.ui[i].level);
                        listViewUsers.Items.Add(lvi);
                    }
                }
            }
            else if (checkBoxAdminShowDivers.Checked == true)
            {
                listViewUsers.Items.Clear();
                for (int i = 0; i < Program.ui.Count; i++)
                {
                    if (Program.ui[i].level == "0")
                    {
                        ListViewItem lvi = new ListViewItem(Program.ui[i].id);
                        lvi.SubItems.Add(Program.ui[i].username);
                        lvi.SubItems.Add(Program.ui[i].firstname);
                        lvi.SubItems.Add(Program.ui[i].lastname);
                        lvi.SubItems.Add(Program.ui[i].age);
                        lvi.SubItems.Add(Program.ui[i].gender);
                        lvi.SubItems.Add(Program.ui[i].nation);
                        lvi.SubItems.Add(Program.ui[i].level);
                        listViewUsers.Items.Add(lvi);
                    }
                }
            }
            else
                listViewUsers.Items.Clear();
            try
            {
                listViewUsers.Items[indexUserList].Selected = true;
            }
            catch (Exception)
            {

            }
            
        }

        private void Main_Load(object sender, EventArgs e)
        {
            this.comboBoxNewGender.SelectedIndex = 0;
            this.comboBoxNewHeight.SelectedIndex = 0;
            this.textBoxNewDate.Text = DateTime.Today.ToShortDateString();
            this.comboBoxNewMaxJudges.SelectedIndex = 0;
            this.radioButtonDiveCode.Checked = true;
            labelTest.Text = listViewUsers.SelectedIndices[0].ToString();
        }

        private void trackBarScore_Scroll(object sender, EventArgs e)
        {
            double x = trackBarScore.Value;
            x = x / 2;
            textBoxScore.Text = x.ToString();
        }

        private void textBoxScore_TextChanged(object sender, EventArgs e)
        {
            if (textBoxScore.Text != null)
            {
                double x = Convert.ToDouble(textBoxScore.Text);
                x = x * 2;
                trackBarScore.Value = Convert.ToInt32(x);
            }
        }

        private void radioButtonCustomDiveCode_CheckedChanged(object sender, EventArgs e)
        {
            if (textBoxDiveCode.Enabled == false)
            {
                textBoxDiveCode.Enabled = true;
                textBoxDiveCode.Text = null;
            }
            else
                textBoxDiveCode.Enabled = false;
        }

        private void radioButtonDiveCode_CheckedChanged(object sender, EventArgs e)
        {
            if (groupBoxDiverDivecode.Enabled == false)
            {
                groupBoxDiverDivecode.Enabled = true;
                textBoxDiveCode.Text = null;
            }
            else
            {
                groupBoxDiverDivecode.Enabled = false;

            }
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if (this.EventLogin != null)
            {
                this.EventLogin(textBoxUsername.Text, textBoxPassword.Text);
                Thread.Sleep(100);
                if (Program.isLoggedIn)
                {
                    groupBoxLogin.Hide();
                    if (Program.cu.level == "0")
                    {
                        tabMain.TabPages.Add(tabPageDiver);
                        RefreshDiverTournamentListview();
                        RefreshDiverCurrentTournamentListview();
                    }
                    else if (Program.cu.level == "1")
                    {
                        tabMain.TabPages.Add(tabPageJudge);
                        RefreshJudgeContestInformation();
                    }
                    else if (Program.cu.level == "2")
                    {
                        tabMain.TabPages.Add(tabPageAdmin);
                        RefreshAdminTournamentListview();
                        RefreshAdminApplicationListview();
                        RefreshAdminUserListview();
                    }
                }
            }
        }

        private void buttonSignUp_Click(object sender, EventArgs e)
        {
            if (EventOpenSignUpWindow != null)
            {
                this.EventOpenSignUpWindow();
            }
        }

        private void buttonCreateTournament_Click(object sender, EventArgs e)
        {
            if (EventNewTournament != null)
            {
                try
                {
                    Tournament t = new Tournament(textBoxNewTitle.Text, calendarNewTournament.SelectionRange.Start.ToShortDateString(), Convert.ToInt32(comboBoxNewHeight.Text), comboBoxNewGender.Text,
                        Convert.ToInt32(comboBoxNewMaxJudges.Text), Convert.ToInt32(textBoxNewMaxDivers.Text));
                    EventNewTournament(t);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Create new tournament failed, please try again.");
                    return;
                }
                MessageBox.Show("Create tournament successful.");
            }
        }

        private void calendarNewTournament_DateChanged(object sender, DateRangeEventArgs e)
        {
            textBoxNewDate.Text = calendarNewTournament.SelectionRange.Start.ToShortDateString();
        }

        private void buttonRefreshUsers_Click(object sender, EventArgs e)
        {
            if (EventRefreshUserList != null)
            {
                this.EventRefreshUserList();
                Thread.Sleep(200);
                RefreshAdminUserListview();
            }
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            if (EventApplyForTournament != null)
            {
                try
                {
                    if (listViewAvailableTournaments.SelectedItems[0].SubItems[3].Text != "0")
                        this.EventApplyForTournament(listViewAvailableTournaments.SelectedItems[0].Text);
                    else
                        MessageBox.Show("This tournament is full!");
                }
                catch (Exception ex)
                {
                }
            }
        }

        private void comboBoxDiveType_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxDiveCode.Text = comboBoxDiveType.Text + comboBoxDiveMotion.Text + comboBoxDiveSomersaults.Text + comboBoxDiveTwists.Text + comboBoxDivePosition.Text;
        }

        private void comboBoxDiveMotion_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxDiveCode.Text = comboBoxDiveType.Text + comboBoxDiveMotion.Text + comboBoxDiveSomersaults.Text + comboBoxDiveTwists.Text + comboBoxDivePosition.Text;
        }

        private void comboBoxDiveSomersaults_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxDiveCode.Text = comboBoxDiveType.Text + comboBoxDiveMotion.Text + comboBoxDiveSomersaults.Text + comboBoxDiveTwists.Text + comboBoxDivePosition.Text;
        }

        private void comboBoxDiveTwists_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxDiveCode.Text = comboBoxDiveType.Text + comboBoxDiveMotion.Text + comboBoxDiveSomersaults.Text + comboBoxDiveTwists.Text + comboBoxDivePosition.Text;
        }

        private void comboBoxDivePosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxDiveCode.Text = comboBoxDiveType.Text + comboBoxDiveMotion.Text + comboBoxDiveSomersaults.Text + comboBoxDiveTwists.Text + comboBoxDivePosition.Text;
        }

        private void buttonAddDiveToList_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < DDList.ddList.Count(); i++)
            {
                if (textBoxDiveCode.Text == DDList.ddList[i].diveCode)
                {
                    if (listBoxDiveCodes.Items.Count < 6)
                    {
                        for (int j = 0; j < listBoxDiveCodes.Items.Count; j++)
                        {
                            if (textBoxDiveCode.Text == listBoxDiveCodes.Items[j].ToString())
                            {
                                labelDiverDifficulty.Text = "This jump is already in the list.";
                                return;
                            }
                        }
                        listBoxDiveCodes.Items.Add(textBoxDiveCode.Text);
                        if (listBoxDiveCodes.Items.Count == 6)
                            buttonAddDiveToList.Enabled = false;
                    }
                    return;
                }
            }
        }

        private void buttonRemoveDive_Click(object sender, EventArgs e)
        {
            try
            {
                listBoxDiveCodes.Items.RemoveAt(listBoxDiveCodes.SelectedIndex);
                buttonAddDiveToList.Enabled = true;

            }
            catch (Exception ex)
            {

            }
        }

        private void timerRefresh_Tick(object sender, EventArgs e)
        {
            if (Program.cu.level == "0")
            {
                RefreshDiverTournamentListview();
                RefreshDiverCurrentTournamentListview();
                RefreshDiverScores();
            }
            else if (Program.cu.level == "1")
            {
                RefreshJudgeContestInformation();
            }
            else if (Program.cu.level == "2")
            {
                RefreshAdminTournamentListview();
                RefreshAdminApplicationListview();
                RefreshAdminUserListview();
            }

            for (int i = 0; i < Program.ti.Count; i++)
            {
                if (Program.ti[i].title == Program.cu.inTournament && Program.ti[i].isFinished == true)
                {
                    Program.cu.inTournament = null;
                    break;
                }
            }
        }

        private void buttonApplyAccept_Click(object sender, EventArgs e)
        {
            if (EventAcceptUserForTournament != null)
            {
                try
                {
                    this.EventAcceptUserForTournament(listViewApply.SelectedItems[0].SubItems[0].Text, listViewApply.SelectedItems[0].SubItems[1].Text);
                    for (int i = 0; i < Program.tr.Count; i++)
                    {
                        if (listViewApply.SelectedItems[0].SubItems[0].Text == Program.tr[i].username &&
                            listViewApply.SelectedItems[0].SubItems[1].Text == Program.tr[i].tournamentTitle)
                        {
                            Program.tr.RemoveAt(i);
                            RefreshAdminApplicationListview();
                            break;
                        }

                    }
                }
                catch (Exception ex)
                {
                }

            }

        }

        private void buttonApplyDecline_Click(object sender, EventArgs e)
        {
            if (EventDeclineUserForTournament != null)
            {
                try
                {
                    this.EventDeclineUserForTournament(listViewApply.SelectedItems[0].SubItems[0].Text, listViewApply.SelectedItems[0].SubItems[1].Text);
                    for (int i = 0; i < Program.tr.Count; i++)
                    {
                        if (listViewApply.SelectedItems[0].SubItems[0].Text == Program.tr[i].username &&
                            listViewApply.SelectedItems[0].SubItems[1].Text == Program.tr[i].tournamentTitle)
                        {
                            Program.tr.RemoveAt(i);
                            RefreshAdminApplicationListview();
                            break;
                        }

                    }
                }
                catch (Exception ex)
                {
                }
            }
        }

        private void buttonRemoveTournament_Click(object sender, EventArgs e)
        {
            if (EventRemoveTournament != null)
            {
                try
                {
                    this.EventRemoveTournament(listViewTournaments.SelectedItems[0].SubItems[1].Text);
                    for (int i = 0; i < Program.ti.Count; i++)
                    {
                        if (listViewTournaments.SelectedItems[0].SubItems[1].Text == Program.ti[i].title)
                        {
                            Program.ti.RemoveAt(i);
                            RefreshAdminTournamentListview();
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

        private void buttonAdminStartTournament_Click(object sender, EventArgs e)
        {

            if (EventStartTournament != null)
            {

                this.EventStartTournament(listViewTournaments.SelectedItems[0].SubItems[1].Text);
                this.EventRefreshTournamentList();
                Thread.Sleep(150);
                RefreshAdminTournamentListview();
                RefreshJudgeContestInformation();
            }
        }

        private void timerConnected_Tick(object sender, EventArgs e)
        {
            if (Program.isConnected)
            {
                tabMain.Visible = true;
                groupBoxMain.Enabled = true;
                pictureBoxConnecting.Visible = false;
                timerConnected.Interval = 60000;
            }
            else
            {
                tabMain.Visible = false;
                groupBoxMain.Enabled = false;
                pictureBoxConnecting.Visible = true;
            }
        }

        private void buttonAdminAddJudgeToTournament_Click(object sender, EventArgs e)
        {
            if (EventAddJudgeToTournament != null)
            {
                this.EventAddJudgeToTournament(listViewUsers.SelectedItems[0].SubItems[1].Text, listViewTournaments.SelectedItems[0].SubItems[1].Text);
                MessageBox.Show(listViewUsers.SelectedItems[0].SubItems[1].Text + " added to " + listViewTournaments.SelectedItems[0].SubItems[1].Text);

            }
        }

        private void listViewUsers_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (listViewUsers.SelectedIndices.Count > 0)
            {
                indexUserList = listViewUsers.SelectedIndices[0];
                if (listViewUsers.SelectedItems[0].SubItems[7].Text != "1")
                    buttonAdminAddJudgeToTournament.Enabled = false;
                else if (listViewUsers.SelectedItems[0].SubItems[7].Text == "1")
                    buttonAdminAddJudgeToTournament.Enabled = true;
            }

        }

        private void listViewTournaments_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewTournaments.SelectedIndices.Count > 0)
                indexTournamentList = listViewTournaments.SelectedIndices[0];
        }

        private void buttonDiverSubmitDive_Click(object sender, EventArgs e)
        {
            if (EventSubmitDive != null)
            {
                List<string> dives = new List<string>();
                for (int i = 0; i < 6; i++)
                {
                    dives.Add(listBoxDiveCodes.Items[i].ToString());
                }
                this.EventSubmitDive(dives, Program.cu.username, Program.cu.inTournament);
            }
        }

        private void buttonSubmitScore_Click(object sender, EventArgs e)
        {
            if (EventSubmitJudgeScore != null)
            {
                try
                {
                    this.EventSubmitJudgeScore(listViewJudgeDiverList.SelectedItems[0].SubItems[3].Text, Program.cu.inTournament, listViewJudgeDiverList.SelectedItems[0].SubItems[1].Text,
                        textBoxScore.Text, Program.cu.round, Program.cu.username);
                }
                catch (Exception ex)
                {
                }
            }
        }

        private void textBoxDiveCode_TextChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < DDList.ddList.Count; i++)
            {
                if (DDList.ddList[i].diveCode == textBoxDiveCode.Text && labelDiverHeightInfo.Text == DDList.ddList[i].diveHeight.ToString())
                {
                    labelDiverDifficulty.Text = "DD: " + DDList.ddList[i].diveDifficulty.ToString();
                    break;
                }
                else if (textBoxDiveCode.Text == "")
                    labelDiverDifficulty.Text = "";
                else
                    labelDiverDifficulty.Text = "This jump cannot be made in this tournament.";
            }
        }

        private void checkBoxAdminShowJudges_CheckedChanged(object sender, EventArgs e)
        {
            RefreshAdminUserListview();
        }

        private void checkBoxAdminShowDivers_CheckedChanged(object sender, EventArgs e)
        {
            RefreshAdminUserListview();
        }

        private void listViewJudgeDiverList_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshJudgeScores();
        }

        private void buttonRefreshTournaments_Click(object sender, EventArgs e)
        {
            if (EventRefreshTournamentList != null)
            {
                this.EventRefreshTournamentList();
                Thread.Sleep(200);
                Program.ti.Sort((x, y) => DateTime.Parse(x.date).CompareTo(DateTime.Parse(y.date)));


                RefreshAdminTournamentListview();

            }
        }
    }
}
